﻿@namespace AntDesign
@using Microsoft.AspNetCore.Components.Rendering
@inherits Tabs
@inject NavigationManager _navigationManager

@{
    base.BuildRenderTree(__builder);
}

@code{
    internal void RenderPanes(RenderTreeBuilder __builder)
    {
        @if (ReuseTabsService.Pages?.Any() == true)
        {
            // Create a stable list to avoid issues with collection changes during rendering
            var pageList = ReuseTabsService.Pages.ToList();
            @foreach (var item in pageList)
            {
                // Capture the item in a local variable to ensure stable closure
                var currentItem = item;
                // Pre-compute the content to ensure stable reference
                var content = GetPaneContent(currentItem);
                var itemUrl = currentItem.Url; // Capture URL for callbacks
                
                <TabPane Id="@currentItem.Url" @key="@currentItem.Key" Key="@currentItem.Key" Class="@TabPaneClass" Closable="currentItem.Closable" ForceRender ChildContent="@content">
                    <TabTemplate>
                        @if (Type == TabType.Line && currentItem.Closable)
                        {
                            <span> @currentItem.Title</span>
                            <Icon Style="margin-right:0;" Type="@IconType.Outline.Close" OnClick="@(()=>{ ClosePage(itemUrl);})" />
                        }
                        else
                        {
                            @currentItem.Title
                        }
                    </TabTemplate>
                    <TabContextMenu>
                        <Menu>
                            <MenuItem OnClick="@(()=>{ReuseTabsService.ReloadPage(itemUrl);})">@Locale.Reload</MenuItem>
                            <MenuItem Disabled="!currentItem.Closable" OnClick="@(()=>{ReuseTabsService.ClosePage(itemUrl);})">@Locale.Close</MenuItem>
                            <MenuItem OnClick="@(()=>ReuseTabsService.CloseAll())">@Locale.CloseAllTabs</MenuItem>
                            <MenuItem OnClick="@(()=>{ ReuseTabsService.CloseOther(itemUrl);})">@Locale.CloseAllButThis</MenuItem>
                            @* <MenuItem OnClick="@(()=>{ ReuseTabsService.Pin(itemUrl);})">@Locale.PinTab</MenuItem> *@
                        </Menu>
                    </TabContextMenu>
                </TabPane>
            }
        }
    }

    private RenderFragment GetPaneContent(ReuseTabsPageItem item)
    {
        if (!HidePages && item.Rendered)
        {
            // Always use cached RenderFragment to ensure reference stability for KeepAlive
            if (item.CachedRenderFragment == null && item.Body != null)
            {
                // Create a stable cached version
                if (TabPaneTemplate != null)
                    item.CachedRenderFragment = TabPaneTemplate(item);
                else
                    item.CachedRenderFragment = item.Body;
            }
            return item.CachedRenderFragment;
        }

        return null;
    }
}